import { getAreaTypes, getAreaCascade, getAreaCascadeSupply } from '@/api/adminArea'
import { getAgencyTypeList, getSchoolsByAreaId, queryConfByType } from '@/api/organize'
import { getSchoolsByTitle } from '@/api/user'

const options = {
  state: {
    areaTypeOptions: [],
    areaCascadeOptions: [],
    areaCascadeSupplyOptions: [],
    organizeTypeOptions: [],
    schoolsOptions: [],
    schoolsByTitleOptions: [],
    universityLabelOptions: []
  },

  actions: {
    GetAreaTypeOptions: ({ commit }) => {
      getAreaTypes()
        .then(({ data }) => {
          commit('SAVE_AREA_TYPES', data)
        })
    },

    GetAreaCascadeOptions: ({ commit }) => {
      getAreaCascade()
        .then(({ data }) => {
          commit('SAVE_AREA_CASCADE', data)
        })
    },

    GetAreaCascadeSupplyOptions: ({ commit }) => {
      return getAreaCascadeSupply()
        .then(({ data }) => {
          commit('SAVE_AREA_CASCADE_SUPPLY', data)
          return data
        })
    },

    GetOrganizeTypeOptions: ({ commit }) => {
      getAgencyTypeList()
        .then(({ data }) => {
          commit('SAVE_ORGANIZE_TYPE', data)
        })
    },

    GetSchoolsOptions: ({ commit }, areaId) => {
      getSchoolsByAreaId({ areaId })
        .then(({ data }) => {
          commit('SAVE_SCHOOLS', data)
        })
    },

    GetSchoolsByTitleOptions: ({ commit }, title) => {
      getSchoolsByTitle({ title })
        .then(({ data }) => {
          commit('SAVE_SCHOOLS_BY_TITLE', data)
        })
    },

    GetUniversityLabelOptions: ({ commit }) => {
      return queryConfByType({ paramType: 'UNIV_FEATURE' })
        .then(({ data }) => {
          commit('SAVE_UNIVERSITY_LABEL', data)
          return data
        })
    }
  },

  mutations: {
    SAVE_AREA_TYPES: (state, types = []) => {
      state.areaTypeOptions = types.map(({ code, msg }) => {
        return {
          value: code,
          label: msg
        }
      })
    },

    SAVE_AREA_CASCADE: (state, area = []) => {
      state.areaCascadeOptions = area
    },

    SAVE_AREA_CASCADE_SUPPLY: (state, area = []) => {
      state.areaCascadeSupplyOptions = area
    },

    SAVE_ORGANIZE_TYPE: (state, types = []) => {
      state.organizeTypeOptions = types.map(({ agencyTypeId, agencyTypeName }) => {
        return {
          value: '' + agencyTypeId,
          label: agencyTypeName
        }
      })
    },

    SAVE_SCHOOLS: (state, types = []) => {
      state.schoolsOptions = types.map(({ schoolId, schoolName }) => {
        return {
          value: '' + schoolId,
          label: schoolName
        }
      })
    },

    SAVE_SCHOOLS_BY_TITLE: (state, types = []) => {
      state.schoolsByTitleOptions = types.map(({ schoolId, schoolName }) => {
        return {
          value: schoolId,
          label: schoolName
        }
      })
    },
    SAVE_UNIVERSITY_LABEL: (state, labels = []) => {
      state.universityLabelOptions = labels
    }
  }
}

export default options
